<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click.native="showDialogs(20)"
            >添加权限</el-button
          >
        </div>
        <el-table border :data="lists" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="text"
                v-if="scope.row.type === 1"
                @click.native="showDialogs(scope.row)"
                >添加</el-button
              >
              <el-button type="text" @click="editPermission(scope.row)"
                >编辑</el-button
              >
              <el-button type="text" @click="delPermission(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <el-dialog :visible="showDialog" :title="title" @close="showDialog = false">
      <el-form label-width="100px" :model="form" :rules="rules" ref="form">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
            v-model="form.enVisible"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click.native="makesure">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermission, delPermission, getPermissionDetail, updatePermission } from '@/api/permissions'
import { tranListToTreeData } from '@/utils/translist'
export default {
  // 组件名称
  name: 'permission',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 局部注册的组件
  components: {},
  // 组件状态值
  data () {
    return {
      title: '添加权限',
      value: '1',
      showDialog: false,
      lists: [],
      form: {
        name: '',
        code: '',
        description: '',
        enVisible: '1',
        type: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入权限名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入权限标识', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请选择权限描述', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getPermissionLists()
  },
  methods: {
    getPermissionLists () {
      getPermissionList().then(res => {
        this.lists = tranListToTreeData(res.data, '0')
      })
    },
    showDialogs (data) {
      this.title = '添加权限'
      if (data == 20) {
        this.form.type = 1
        this.form.pid = 0
        this.showDialog = true
        return
      }
      this.form.type = 2
      this.form.pid = data.id
      this.showDialog = true
    },
    makesure () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (!this.form.id) {
            addPermission(this.form).then(res => {
              this.$message.success('添加成功')
              this.showDialog = false
              this.form = {
                name: '',
                code: '',
                description: '',
                enVisible: '',
                type: '',
                pid: ''
              }
              this.$refs.form.resetFields();
              this.getPermissionLists()
            })
          }
          else {
            updatePermission(this.form).then(res => {
              this.$refs.form.resetFields();
              this.getPermissionLists()
              this.$message.success('更新成功')
              this.showDialog = false
            })
          }
        } else {
          return false;
        }
      });
    },
    delPermission (data) {
      this.$confirm('确定删除权限吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delPermission(data.id).then(res => {
          this.$message.success('删除成功')
          this.getPermissionLists()
        })
      }).catch(() => { });
    },
    editPermission (data) {
      this.title = '编辑权限'
      getPermissionDetail(data.id).then(res => {
        this.showDialog = true
        this.form = res.data
      })
    }
  }
}
</script>
<style scoped>
</style>
